﻿#region Copyright ©2008-2011, Technische Universitaet Muenchen
// ====================================================================================================
//
// Last Changed by       $Author: then $
// Last Changed Date     $LastChangedDate: 2009-06-15 08:45:45 +0200 (Mon, 15 Jun 2009) $
// Last Changed Revision $Rev: 194 $
//
// ====================================================================================================
#endregion

using System.Windows;
using Microsoft.Win32;
using System;

namespace Tum.CollabXT.Wizard
{
    /// <summary>
    /// Interaction logic for FinishedPage.xaml
    /// </summary>
    internal partial class FinishedPage : InternalConversionWorkflowPage
    {
        /// <summary>
        /// Used conversion workflow.
        /// </summary>
        ConversionWorkflow Workflow;


        /// <summary>
        /// Constructor.
        /// </summary>
        public FinishedPage()
        {
            InitializeComponent();

            Helper.FillInlineCollectionFromString(textBlockFinishedMessage.Inlines, Wizard.Resources.Language.FinishedPage_FinishedMessage);
        }

        /// <summary>
        /// Receives the conversion workflow.
        /// </summary>
        /// <param name="workflow">Conversion workflow.</param>
        /// <returns>True, if the workflow has been received correctly, false if not.</returns>
        public override bool InternalReceiveWorkflowHandle(ConversionWorkflow workflow)
        {
            Workflow = workflow;
            Workflow.UpdateButtonState(true, false);
            return true;
        }

        /// <summary>
        /// Event handler for the save button.
        /// </summary>
        private void buttonSaveProject_Click(object sender, RoutedEventArgs e)
        {
            /*    Uncomment to change from "Save as .." to "Save" functionality
             * try
            {
                Workflow.Project.Save(null);
            }
            catch (CXTWizardException)
            {*/

            buttonSaveProject.Content = Wizard.Resources.Language.FinishedPage_SaveProject;

            SaveFileDialog saveFileDlg = new SaveFileDialog();
            saveFileDlg.Filter = Wizard.Resources.Language.ProjectFilesFilter + " (*.ppf)|*.ppf";
            saveFileDlg.RestoreDirectory = true;
            saveFileDlg.AddExtension = true;
            saveFileDlg.OverwritePrompt = true;

            if (saveFileDlg.ShowDialog() == true)
            {
                bool successful = false;
                try
                {
                    Workflow.Project.Save(saveFileDlg.FileName);
                    successful = true;
                }
                catch(Exception) { }
                buttonSaveProject.Content = Wizard.Resources.Language.FinishedPage_SaveProject + " " + (successful ? Wizard.Resources.Language.FinishedPage_SaveCompleted : Wizard.Resources.Language.FinishedPage_SaveFailed);
            }
        }

        /// <summary>
        /// Event handler for the close button.
        /// </summary>
        private void buttonCloseWizard_Click(object sender, RoutedEventArgs e)
        {
            Workflow.CloseWizard();
        }
    }
}
